package com.yanqu.road.server.pb;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.houbb.heaven.util.util.DateUtil;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.*;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

public class UnionPb {

    public static UnionProto.UnionDetailMsg.Builder parseUnionDetailMsg(String unionUid, UnionInfo unionInfo, List<UnionMember> memberList, int syncType, List<UnionFlag> flagList){
        UnionProto.UnionDetailMsg.Builder msg = UnionProto.UnionDetailMsg.newBuilder();
        msg.setType(syncType);
        msg.setUnionUid(unionUid);
        UnionProto.UnionTempMsg.Builder unionMsg = parseUnionTempMsg(unionInfo);
        if(null != unionMsg){
            msg.setUnionInfo(unionMsg);
        }
        if(null != memberList){
            for(UnionMember unionMember : memberList){
                UnionProto.UnionMemberTempMsg.Builder memberMsg = parseUnionMemberTempMsg(unionMember);
                if(null != memberMsg){
                    msg.addMemberList(memberMsg);
                }
            }
        }
        if (flagList != null) {
            for (UnionFlag flag : flagList) {
                msg.addFlag(parseFlagMsg(flag));
            }
        }
        return msg;
    }

    public static UnionProto.UnionFlagMsg.Builder parseFlagMsg(UnionFlag flag) {
        UnionProto.UnionFlagMsg.Builder msg = UnionProto.UnionFlagMsg.newBuilder();
        msg.setFlagId(flag.getFlagId());
        msg.setExpiredTime(DateHelper.toSecond(flag.getExpireTime()));
        msg.setRead(flag.isRead());
        return msg;
    }

    public static UnionProto.UnionTempMsg.Builder parseUnionTempMsg(UnionInfo unionInfo){
        if(null == unionInfo){
            return null;
        }
        UnionProto.UnionTempMsg.Builder msg = UnionProto.UnionTempMsg.newBuilder();
        msg.setUnionUid(unionInfo.getUnionUid());
        msg.setUnionName(unionInfo.getUnionName());
        msg.addUnionFlag(unionInfo.getUnionFlag());
        msg.setExperience(unionInfo.getExperience());
        msg.setOutNotice(unionInfo.getOutNotice());
        msg.setIsAllowRandomJoin(unionInfo.isAllowRandomJoin());
        msg.setInnerNotice(unionInfo.getInnerNotice());
        msg.setWtCode(unionInfo.getWechatCode());
        msg.setQqCode(unionInfo.getQqCode());
        msg.setOwnerUserId(unionInfo.getMasterUserId());
        msg.setOwnerNickName(unionInfo.getMasterNickName());
        msg.setWealth(unionInfo.getWealth());
        msg.setMemberCount(unionInfo.getMemberCount());
        msg.setTotalEarnSpeed(unionInfo.getTotalEarnSpeed().toString());
        msg.setIsDelete(unionInfo.isDelete());
        msg.setFlagModifyTime(unionInfo.getFlagModifyTime());
        msg.setConstructActive(unionInfo.getConstructActive());
        msg.setUnionAliasId(unionInfo.getUnionAliasId());
        msg.setApplyTitleLimit(unionInfo.getApplyTitleLimit());
        return msg;
    }

    public static UnionProto.UnionMemberTempMsg.Builder parseUnionMemberTempMsg(UnionMember unionMember){
        if(null == unionMember){
            return null;
        }
        UnionProto.UnionMemberTempMsg.Builder msg = UnionProto.UnionMemberTempMsg.newBuilder();
        msg.setUnionUid(unionMember.getUnionUid());
        msg.setPosition(unionMember.getPosition());
        msg.setUserId(unionMember.getUserId());
        UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(unionMember.getUserId(), GameServer.getInstance().getServerId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg;
        if(userBaseInfo == null){
            baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(unionMember.getUserBaseInfo());
        }else{
            baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo);
        }
        msg.setPlayerBaseData(baseMsg);
        msg.setContribution(unionMember.getContribution());
        msg.setTotalContribution(unionMember.getTotalContribution());
        msg.setDonateTimes(unionMember.getDonateTimes());
        msg.setQuitTime(unionMember.getQuitTime());
        msg.addAllApplyUnionUidList(StringUtils.stringToStringList(unionMember.getApplyUnionUidListString(), ","));
        if(!StringUtils.isNullOrEmpty(unionMember.getUnionConstructMapJson())){
            Map<Integer, UnionConstruct> unionConstructMap = JSON.parseObject(unionMember.getUnionConstructMapJson(), new TypeReference<Map<Integer, UnionConstruct>>() {});
            for(UnionConstruct unionConstruct : unionConstructMap.values()){
                UnionProto.UnionConstructTempMsg.Builder constructMsg = parseUnionConstructTempMsg(unionConstruct);
                if(null != constructMsg){
                    msg.addConstructList(constructMsg);
                }
            }
        }
        msg.setTodayContribution(unionMember.getTodayContribution());
        msg.addAllConstructRewardIndexList(StringUtils.stringToIntegerList(unionMember.getConstructRewardIndexListString(), ","));
        msg.setNoticeReadState(unionMember.getNoticeReadState());
        return msg;
    }

    public static UnionProto.UnionConstructTempMsg.Builder parseUnionConstructTempMsg(UnionConstruct unionConstruct){
        if(null == unionConstruct){
            return null;
        }
        UnionProto.UnionConstructTempMsg.Builder msg = UnionProto.UnionConstructTempMsg.newBuilder();
        msg.setConstructType(unionConstruct.getConstructType());
        msg.setTimes(unionConstruct.getTimes());
        msg.setLastConstructTime(unionConstruct.getLastConstructTime());
        return msg;
    }

    public static UnionProto.UnionJoinApplyTempMsg.Builder parseUnionJoinApplyTempMsg(UnionJoinApply unionJoinApply){
        if(null == unionJoinApply){
            return null;
        }
        UnionProto.UnionJoinApplyTempMsg.Builder msg = UnionProto.UnionJoinApplyTempMsg.newBuilder();
        msg.setUserId(unionJoinApply.getUserId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(unionJoinApply.getUserBaseInfo());
        msg.setPlayerBaseData(baseMsg);
        msg.setApplyTime(unionJoinApply.getApplyTime());
        msg.setIsDelete(unionJoinApply.isDelete());
        return msg;
    }

    public static UnionProto.UnionJoinApplyListMsg.Builder parseUnionJoinApplyListMsg(List<UnionJoinApply> unionJoinApplyList){
        UnionProto.UnionJoinApplyListMsg.Builder listMsg = UnionProto.UnionJoinApplyListMsg.newBuilder();
        if(null != unionJoinApplyList){
            for(UnionJoinApply unionJoinApply : unionJoinApplyList){
                UnionProto.UnionJoinApplyTempMsg.Builder msg = parseUnionJoinApplyTempMsg(unionJoinApply);
                if(null != msg){
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }

    public static UnionProto.UnionNoticeTempMsg.Builder parseUnionNoticeTempMsg(UnionNotice unionNotice){
        if(null == unionNotice){
            return null;
        }
        UnionProto.UnionNoticeTempMsg.Builder msg = UnionProto.UnionNoticeTempMsg.newBuilder();
        msg.setType(unionNotice.getType());
        msg.setUserId(unionNotice.getUserId());
        msg.setNickName(unionNotice.getNickName());
        msg.setNoticeTime(unionNotice.getNoticeTime());
        msg.setContent(unionNotice.getContent());
        return msg;
    }

    public static UnionProto.UnionNoticeListMsg.Builder parseUnionNoticeListMsg(List<UnionNotice> unionNoticeList){
        UnionProto.UnionNoticeListMsg.Builder listMsg = UnionProto.UnionNoticeListMsg.newBuilder();
        if(null != unionNoticeList){
            for(UnionNotice unionNotice : unionNoticeList){
                UnionProto.UnionNoticeTempMsg.Builder msg = parseUnionNoticeTempMsg(unionNotice);
                if(null != msg){
                    listMsg.addDataList(msg);
                }
            }
        }
        return listMsg;
    }

    public static UnionProto.UnionChamberPatronsTempMsg.Builder parseUnionChamberPatronsTempMsg(long userId, boolean isRemove, UserPatrons userPatrons){
        UnionProto.UnionChamberPatronsTempMsg.Builder msg = UnionProto.UnionChamberPatronsTempMsg.newBuilder();
        msg.setUserId(userId);
        msg.setIsRemove(isRemove);
        PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
        if(null != patronsMsg){
            msg.setPatronsData(patronsMsg);
            msg.setPatronsAbility(userPatrons.getAbility().toString());
        }
        return msg;
    }

    public static UnionProto.UnionChamberPatronsListMsg.Builder parseUnionChamberPatronsListMsg(List<UserPatrons> userPatronsList){
        UnionProto.UnionChamberPatronsListMsg.Builder listMsg = UnionProto.UnionChamberPatronsListMsg.newBuilder();
        if(null != userPatronsList){
            for(UserPatrons userPatrons : userPatronsList){
                UnionProto.UnionChamberPatronsTempMsg.Builder msg = parseUnionChamberPatronsTempMsg(userPatrons.getUserId(), false, userPatrons);
                listMsg.addDataList(msg);
            }
        }
        return listMsg;
    }

    public static UnionProto.UnionChamberDataMsg.Builder parseUnionChamberDataMsg(UserUnionChamberData userUnionChamberData){
        UnionProto.UnionChamberDataMsg.Builder msg = UnionProto.UnionChamberDataMsg.newBuilder();
        if (userUnionChamberData != null) {
            msg.addAllUseHuntUserIdList(userUnionChamberData.getUseHuntUserIdList());
        }
        return msg;
    }
}
